

// import * as THREE from '../libs/three.module.js';
// import {GLTFLoader} from '../libs/GLTFLoader.js';
// import {OrbitControls} from '../libs/OrbitControls.js';


import * as THREE from '../../build/three.module.js';
// import { GLTFLoader } from '../../jsm/loaders/GLTFLoader.js';
import { FBXLoader } from '../../jsm/loaders/FBXLoader.js';
import { OrbitControls } from '../../jsm/controls/OrbitControls.js';
// import { OrbitControls } from '../../jsm/controls/OrbitControls.js';


// var modelPath = '../../model/monkey.gltf';
// var modelPath = '../../model/10kV-transfer-inside.gltf';
// var modelPath = '../../model/220kV-3-main-transfer.gltf';
// var modelPath = '../../model/guangfu-test.gltf';
var modelPath = '../../model/para110.FBX';
var modelPath = '../../model/guangfu-fbx.fbx';

var containerDom = document.querySelector("#container");

var width = window.innerWidth,
  height = window.innerHeight;

// Create a renderer and add it to the DOM.
var renderer = new THREE.WebGLRenderer();
renderer.setSize(width, height);
renderer.setClearColor(0xcccccc);
//document.body.appendChild(renderer.domElement);
containerDom.appendChild(renderer.domElement);
// Create the scene 
var scene = new THREE.Scene();
// Create a camera
var camera = new THREE.PerspectiveCamera(45, width / height, 0.1, 10000);
//var camera = new THREE.OrthographicCamera(-1000,1000,1000,-1000,0.1,10000);
camera.position.z = 50;

scene.add(camera);

// Create a light, set its position, and add it to the scene.
var light = new THREE.PointLight(0xffffff);
light.position.set(100, 100, 100);
scene.add(light);

// Add OrbitControls so that we can pan around with the mouse.
//var controls = new THREE.OrbitControls(camera, renderer.domElement);
var controls = new OrbitControls(camera, renderer.domElement);

// Add axes
var axes = new THREE.AxisHelper(50);
scene.add(axes);
 
//================================================= 
// var geometry = new THREE.BoxGeometry(5,5,1);
// var cubeMaterial = new THREE.MeshNormalMaterial(); 

// var mesh = new THREE.Mesh( geometry, cubeMaterial );
// mesh.position.set(1,3,0);
// mesh.scale.multiplyScalar(4);
// mesh.rotation.x = Math.PI/2;
// scene.add( mesh ); 
//=================================================  
// var gltfGroup = new THREE.Group();
// const loader = new FBXLoader();
// loader.load(
//   modelPath,
//   (fbx) => { 

//     scene.add(fbx.scene);    //下面的group操作可以替代仅仅把模型空间载入的动作 
 
//     //将所有加载的设备子内容都加入group  然后把group加入场景
//     for (let i = 0; i < fbx.scene.children.length; i++)
//     {
//       let item = fbx.scene.children[i];
//       gltfGroup.add(item); 
//     }
//     scene.add(gltfGroup);
//     console.log(gltfGroup); 

//     //renderer
//     renderer.render(scene, camera); 
//   },
//   function (xhr) {
//     console.log((xhr.loaded / xhr.total) + '% loaded');
//   },
//   function (error) {
//     console.log('error'+error);
//   }
// );

var theMaterial = new THREE.MeshLambertMaterial({ color: 0xf22fff,side:THREE.DoubleSide });
   
var fbxGroup = new THREE.Group();
var loader = new  FBXLoader();
  loader.load( modelPath, function ( object ) {  
      // object.mixer = new THREE.AnimationMixer( object );
      // mixers.push( object.mixer );
  
      // var action = object.mixer.clipAction( object.animations[ 0 ] );
      // action.play(); 
      object.traverse( function ( child ) { 
          if ( child.isMesh ) {          
              child.castShadow = true;
              child.receiveShadow = true;          
          }       
          if(child instanceof THREE.Mesh && !child.name.indexOf('_')){
            child.material = theMaterial; console.log( child.name)
          }  
      } );    
      fbxGroup.add(object);  
      //scene.add( object );     
  } );
scene.add( fbxGroup ); 
//================================================= 
// var material = new THREE.MeshLambertMaterial({ color: 0x112662 });
// material.side = THREE.DoubleSide;
// // var mesh = new THREE.Mesh(geometry, material);
// for (let i = 0; i < scene.children.length; i++)
// {
//   // let item = scene.children[i];
//   scene.children[i].material = material; 
// }


resize();
animate();
window.addEventListener('resize', resize);

function resize() {
  let w = window.innerWidth;
  let h = window.innerHeight;

  renderer.setSize(w, h);
  camera.aspect = w / h;
  camera.updateProjectionMatrix();
}

fbxGroup.position.set(0.0,-4500, -0);
fbxGroup.rotation.x = Math.PI*0.5;
fbxGroup.rotation.y = Math.PI  ;
// Renders the scene
function animate() { 

  //fbxGroup.position.y-=5;
  //console.log( fbxGroup.position.y)
   //fbxGroup.rotation.y += 0.1;
  // console.log(gltfGroup.rotation.x);

  renderer.render(scene, camera); 
  controls.update();

  requestAnimationFrame(animate);
 
}

//containerDom.appendChild( renderer.domElement );
